package com.ccsoft.zknote.controller;

import cn.hutool.core.lang.Validator;
import cn.hutool.crypto.SecureUtil;
import com.ccsoft.zknote.exception.ServiceException;
import com.ccsoft.zknote.pojo.Thing;
import com.ccsoft.zknote.pojo.system.JsonResult;
import com.ccsoft.zknote.service.IThingService;
import com.ccsoft.zknote.service.IUserService;
import com.ccsoft.zknote.util.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.Map;

/**
 * @author chanchaw
 * @create 2023-07-09 20:19
 */
@RestController
@RequestMapping("/thing")
public class ThingController {
    @Autowired
    private IThingService service;

    @PostMapping("/getUserThings")
    public JsonResult getUserThings(@RequestHeader("token") String token){
        return JsonResult.ok(service.getUserThings8Token(token));
    }

    @PostMapping("/setTop")
    public JsonResult setTop(@RequestBody Map<String,Object> params){
        Integer id = (Integer) params.get("id");
        Boolean top = (Boolean) params.get("top");
        return JsonResult.ok(service.setTop(top, id));
    }

    @PostMapping("/delete8Id")
    public JsonResult delete8Id(@RequestBody Map<String, Object> params){
        Integer id = (Integer) params.get("id");
        Boolean thoroughly = (Boolean) params.get("thoroughly");
        return JsonResult.ok(service.delete8Id(id,thoroughly));
    }

    @PostMapping("/selectByPrimaryKey")
    public JsonResult selectByPrimaryKey(@RequestBody Map<String,Integer> params){
        Integer id = params.get("id");
        return JsonResult.ok(service.selectByPrimaryKey(id));
    }

    @PostMapping("/insert")
    public JsonResult insert(@RequestBody Thing record, @RequestHeader String token) {
        return JsonResult.ok(service.insertSelective(record, token));
    }

    @PostMapping("/update")
    public JsonResult update(@RequestBody Thing record){
        return JsonResult.ok(service.updateSelective(record));
    }

    @PostMapping("/search")
    public JsonResult search(@RequestBody Map<String,Object> params,@RequestHeader String token){
        String search = (String) params.get("search");
        Integer finished = (Integer) params.get("finished");
        return JsonResult.ok(service.search(search,finished,token));
    }
}
